<?php
namespace app\index\controller;

use service\DataService;
use think\Db;
use think\Controller;

/**
 * @desc 代驾信息
 * Class Valet
 * @package app\index\controller
 */
class Valet extends Controller
{
    public $table = 'ValetInformation';

    public function createValet(){

        return view();
    }

    public function registerDo(){
        $datas = $this->request->post();
//        echo '<pre>';
//        print_r($datas['areas']);
//
//        echo '</pre>';
//        exit('-');
//        $area = [
//            ['province_id'=>1,'city_id'=>2,'county_id'=>[6,7,8,9]],
//            ['province_id'=>11,'city_id'=>12,'county_id'=>[16,17,18,19]],
//        ];
        $area = $datas['areas'];
        unset($datas['areas']);
        $areas = [];

        $datas['create_time'] = date('Y-m-d H:i:s');

        $datas['name'] = '1';
        $datas['desc'] = '3';

        $company_id = DataService::save(Db::name($this->table), $datas, 'id', []);

        if($company_id){
            foreach ($area as $k => $v){
                foreach ($v['county_id'] as $kk => $vv){
                    $areas[] = [
                        'province_id' => $v['province_id'],
                        'city_id'     => $v['city_id'],
                        'county_id'   => $vv,
                        'content_id'  => $company_id,
                        'type'        => 1,
                        'create_time' => date('Y-m-d H:i:s')
                    ];
                }
            }

            Db::table('area_relation')->insertAll($areas);

            $data = [
                'code' => 200,
                'msg'   => '注册成功了！',
            ];
        }else{
            $data = [
                'code' => 0,
                'msg'   => '注册失败',
            ];
        }

        return json($data);

    }

    public function uploads(){
        return uploads();
    }

}
